package com.yunfei.yunmusic.web.control;

import com.yunfei.yunmusic.web.model.Comment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
public class CommentPostControl{
private  static Map<String, List<Comment>> comments=new HashMap<>();
@RequestMapping(path="/comment/post")
public  Comment post(@RequestParam(value="songId") String songId,
              Comment comment){
Date commentTime=new Date();
    comment.setCommentTime(commentTime);
    //创建一个绝对唯一的Id
    comment.setId(UUID.randomUUID().toString());
    //將songId的評論 comment放到comments裡面
    comments.computeIfAbsent(songId,k->new ArrayList<>()).add(comment);
    List<Comment> commentList=comments.get(songId);
    Collections.sort(commentList, new Comparator<Comment>() {
        @Override
        public int compare(Comment o1, Comment o2) {
            if(o1.getCommentTime().after(o2.getCommentTime())){
                return -1;
            }
                return 1;

        }
    });
//    return "redirect:/songinfo?songId="+songId;
    return comment;
    }
    public List<Comment> getComments(String songId){
    return comments.get(songId);
    }

}
